/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.groupb.circus.impl_interfaces;

import com.groupb.circus.Databases.Images;
import com.groupb.circus.UserInterface.UserInterfaceP;
import com.groupb.circus.interfaces.SearchProfile;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

/**
 *
 * @author Alex
 */
public class ListenerImpl implements com.groupb.circus.interfaces.Listener, PacketListener {

    protected final static String userContent = "USER_CONTENT";
    protected final static String searchProfile = "SEARCH_PROFILE";
    protected final static String searchResult = "SEARCH_RESULT";
    InstantMessage im = null;
    UserInterfaceP ui = null;
    SearchResultImpl QueryResult = null;
    private static Log log = LogFactory.getLog(ListenerImpl.class);

    public ListenerImpl(InstantMessage im) {
        this.im = im;
    }

    @Override
    public void processPacket(Packet packet) {

        if (packet instanceof Message) {
            Message msg = (Message) packet;

            log.info("New Packet (PacketID: " + msg.getPacketID() + ") from " + msg.getFrom());

          


            if (msg.getPropertyNames().toString().equalsIgnoreCase("[SEARCHPROFILE]")) {
                try {
                    
                  
                  QueryResult = im.ProcessSearchProfile(msg.getFrom(), msg); // FROM O TO ??
                  
                  im.SendSearchResult(msg.getFrom(), QueryResult);
                  
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            
            }
            else if (msg.getPropertyNames().toString().equalsIgnoreCase("[SEARCHRESULT]")) {

              
                   
                  
                  
                
                  im.ProcessSearchResult(msg);
            } 
 
            else if (msg.getPropertyNames().toString().equalsIgnoreCase("[USERCONTENT]")) {
                
                
                im.ProcessUserContent(msg);
                
            } 
            
            else if (msg.getPropertyNames().toString().equalsIgnoreCase("[CHATMESSAGE]")){
                
                im.ProcessChat(msg);

            }
        }
    }
    }
